package com.wx.activitys.drp.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wx.activitys.drp.mapper.PlatfromMapper;
import com.wx.activitys.drp.mapper.ProfitMapper;
import com.wx.activitys.drp.mapper.RoleMapper;
import com.wx.activitys.drp.mapper.ShareMapper;
import com.wx.activitys.drp.mapper.UserInfoMapper;
import com.wx.activitys.drp.model.BPlatfrom;
import com.wx.activitys.drp.model.BProfit;
import com.wx.activitys.drp.model.BRole;
import com.wx.activitys.drp.model.BShare;
import com.wx.activitys.drp.model.BUserInfo;
import com.wx.activitys.drp.service.ShareService;

@Service("/ShareService")
public class ShareServiceImpl implements ShareService {
    @Autowired
    ShareMapper shareMapper;
    @Autowired
    ProfitMapper profitMapper;
    @Autowired
    PlatfromMapper pf;
    @Autowired
    RoleMapper rl;
    
    @Autowired
    UserInfoMapper userInfoMapper;

	@Override
	public List<BShare> findByOpenid(String openid) {
		// TODO Auto-generated method stub
		return shareMapper.findByOpenid(openid);
	}

	@Override
	public void initData(String openid, String pId,String roleKey) {
		List<BShare> list = new ArrayList<>();
		List<BProfit> bProfits=profitMapper.findByRoleKey(roleKey);
		for (BProfit bProfit : bProfits) {
			BShare bs = new BShare();
			bs.setOpenid(openid);
			bs.setDefaultValue(bProfit.getValue());
			bs.setPlatfromId(bProfit.getPaltformId());
			bs.setType(bProfit.getType());
		    list.add(bs);
		}
		shareMapper.addBatch(list);
	}
	
	
	/**
	 * 数据初始化角色平台表
	 */
	@Override
	public void initData1() {
		List<BProfit> list = new ArrayList<>();
		BPlatfrom entity = new BPlatfrom();
		List<BPlatfrom> bp =pf.findByEntity(entity);
		List<BRole> ro = rl.findAll();
		for (BPlatfrom bPlatfrom : bp) {
			list.clear();
			for (BRole bRole : ro) {
				BProfit bs = new BProfit();
				bs.setRoleKey(bRole.getRoleKey());
				bs.setPaltformId(bPlatfrom.getId());
				bs.setValue(1.0);
                list.add(bs);				
			}
			profitMapper.addBatch(list);
		}
	}
	
	/**
	 * 初始化share表
	 */
	@Override
	public void initData2() {
		BUserInfo bu = new BUserInfo();
		List<BShare> list = new ArrayList<>();
		List<BUserInfo> bUserInfos = userInfoMapper.findAll(bu);
		List<BProfit> bProfits=profitMapper.findByRoleKey("1");
		for (BProfit bProfit : bProfits) {
			list.clear();
			for (BUserInfo bus : bUserInfos) {
				BShare bs = new BShare();
				bs.setPlatfromId(bProfit.getPaltformId());
				bs.setDefaultValue(1.0);
				bs.setValue(0.0);
				bs.setOpenid(bus.getOpenid());
				list.add(bs);
			}
			shareMapper.addBatch(list);
		}
	}

	@Override
	public int insert(BShare bShare) {

		return shareMapper.insert(bShare);
	}

	@Override
	public int update(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return shareMapper.update(map);
	}
}
